package me.heshang.rednamedrop;

import me.heshang.rednamedrop.Connection.MySql;
import org.bukkit.Bukkit;
import org.bukkit.entity.*;
import org.bukkit.event.*;
import org.bukkit.event.entity.*;

import java.sql.*;
import java.util.List;


public class Drop implements Listener {
    public static List<String> World = null;
    public static int Lever;
    public static Player player;
    public static String P;
    public static String NAME;

    @EventHandler
    public void Drop(EntityDeathEvent e) throws SQLException, ClassNotFoundException {
        new MySql().getSql();
        LivingEntity b = e.getEntity();
        if (e.getEntity() instanceof Player){
            Player player = (Player) e.getEntity();
            P = player.getName();
            Drop.player = player;
            for (int i = 0; i < World.size(); i++) {
                if(player.getWorld().getName().equals(World.get(i))){
                    return;
                }
            }
            int rowCount = 0;
            int i = 0;
            String sql = "SELECT * FROM  "+MySql.getTabName();
            ResultSet rs = new MySql().getSt().executeQuery(sql);
            while (rs.next()){
                rowCount++;
                NAME = rs.getString("name");
                int lever = rs.getInt("lever");
                if(NAME.equals(P)){
                    if (lever >=Lever){
                        new DropItem().DropItem(player);
                        return;
                    }
                }else {
                    i++;
                }
            }
            if (rowCount == i){
                new MySql().insert();
                System.out.println("玩家:"+P+"不存在将进行添加");
            }
                new MySql().getShut();
                return;
        }else {
            return;
        }
    }
    public static String getP() {
        return P;
    }
    public static void setP(String P) {
        Drop.P = P;
    }
    public static List<String> getWorld() {
        return World;
    }
    public static void setWorld(List<String> World) {
        Drop.World = World;
    }
}
